package com.haixiaoke.saas.system.framework.manager.factory;


import com.haixiaoke.saas.bill.service.IHouseBillService;
import com.haixiaoke.saas.bill.service.IProprietorBillService;
import com.haixiaoke.saas.common.constant.Constants;
import com.haixiaoke.saas.common.utils.LogUtils;
import com.haixiaoke.saas.common.utils.ServletUtils;
import com.haixiaoke.saas.common.utils.StringUtils;
import com.haixiaoke.saas.common.utils.ip.AddressUtils;
import com.haixiaoke.saas.common.utils.ip.IpUtils;
import com.haixiaoke.saas.common.utils.spring.SpringUtils;
import com.haixiaoke.saas.common.weChat.domain.ParseParam;
import com.haixiaoke.saas.common.weChat.domain.RefundParam;
import com.haixiaoke.saas.common.weChat.domain.RefundReqInfo;
import com.haixiaoke.saas.messageCenter.domain.OrdinaryMessage;
import com.haixiaoke.saas.messageCenter.service.IOrdinaryMessageService;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.order.service.ICheckOutOrderService;
import com.haixiaoke.saas.order.service.IOrderService;
import com.haixiaoke.saas.system.domain.SysLogininfor;
import com.haixiaoke.saas.system.domain.SysOperLog;
import com.haixiaoke.saas.system.service.ISysLogininforService;
import com.haixiaoke.saas.system.service.ISysOperLogService;
import com.haixiaoke.saas.workOrder.service.IWorkOrderService;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 * @author haixiaoke
 */
public class AsyncFactory {
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息
     * @param args     列表
     * @return 任务task
     */
    public static TimerTask recordLogininfor(final String username, final String status, final String message,
                                             final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr();
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(address);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                sys_user_logger.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLogininfor logininfor = new SysLogininfor();
                logininfor.setUserName(username);
                logininfor.setIpaddr(ip);
                logininfor.setLoginLocation(address);
                logininfor.setBrowser(browser);
                logininfor.setOs(os);
                logininfor.setMsg(message);
                // 日志状态
                if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
                    logininfor.setStatus(Constants.SUCCESS);
                } else if (Constants.LOGIN_FAIL.equals(status)) {
                    logininfor.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringUtils.getBean(ISysLogininforService.class).insertLogininfor(logininfor);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final SysOperLog operLog) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
                SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);
            }
        };
    }

    /**
     * 微信支付订单回调业务
     *
     * @param parseParam
     * @return
     */
    public static TimerTask weChatCallbackOrderPay(final ParseParam parseParam) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IOrderService.class).payCallbackOrder(parseParam);
            }
        };
    }

    /**
     * 微信缴费订单回调业务
     *
     * @param parseParam
     * @return
     */
    public static TimerTask weChatCallbackOrderSupplementaryPay(final ParseParam parseParam) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(ICheckOutOrderService.class).supplementaryPayCallbackOrder(parseParam);
            }
        };
    }

    /**
     * 退订异步线程任务
     */
    public static TimerTask weChatCallbackCancelReservation(RefundParam refundParam, RefundReqInfo reqInfo) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IOrderService.class).cancelReservationCallbackOrder(refundParam, reqInfo);
            }
        };
    }

    /**
     * 退房异步线程任务
     */
    public static TimerTask weChatCallbackCheckOut(RefundParam refundParam, RefundReqInfo reqInfo) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IOrderService.class).checkOutCallbackOrder(refundParam, reqInfo);
            }
        };
    }

    /**
     * 微信支付业主账单回调业务
     *
     * @param parseParam
     * @return
     */
    public static TimerTask weChatCallbackProprietorBillPay(final ParseParam parseParam) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IProprietorBillService.class).payCallbackBill(parseParam);
            }
        };
    }

    /**
     * 微信支付入住账单回调业务
     *
     * @param parseParam
     * @return
     */
    public static TimerTask weChatCallbackHouseBillPay(final ParseParam parseParam) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IHouseBillService.class).payCallbackBill(parseParam);
            }
        };
    }

    /**
     * 翼码支付回调业务
     *
     * @param houseBill
     * @return
     */
    public static TimerTask yiMaCallbackHouseBillPay(final Map<String, Object> houseBill) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IHouseBillService.class).yiMaPyCallbackBill(houseBill);
            }
        };
    }

    /**
     * 短租订单续租支付成功回调授权智能锁人脸业务
     *
     * @param order
     * @return
     */
    public static TimerTask authorizedFace(final Order order) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IOrderService.class).authorizedFace(order);
            }
        };
    }

    /**
     * 工单扫码支付回调
     * @param parseParam
     * @return
     */
    public static TimerTask weChatCallbackWorkOrderNativePay(ParseParam parseParam) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IWorkOrderService.class).weChatCallbackWorkOrderNativePay(parseParam);
            }
        };
    }

    /**
     * 工单小程序支付回调
     * @param parseParam
     * @return
     */
    public static TimerTask weChatCallbackWorkOrderUnifiedPay(ParseParam parseParam) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IWorkOrderService.class).weChatCallbackWorkOrderUnifiedPay(parseParam);
            }
        };
    }

    /**
     * 发送普通消息
     * @param ordinaryMessage
     * @return
     */
    public static TimerTask sendOrdinaryMessage(OrdinaryMessage ordinaryMessage) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(IOrdinaryMessageService.class).sendOrdinaryMessage(ordinaryMessage);
            }
        };
    }
}
